<?php
// 这是系统自动生成的公共文件
define('config', ['1', '2']);
define('THINK_VERSION', 'v6.0.7');
//数组返回
function getBack($code = '', $msg = '', $data = '', $count = '')
{
    switch ($code) {
        case 0:
            return json(['code' => 0, 'msg' => '操作成功', 'data' => $data, 'count' => $count]);
            break;
        case 1:
            return json(['code' => 100, 'val' => $msg, 'data' => $data, 'count' => $count]);
            break;
        case 2:
            return json(['code' => 200, 'msg' => '操作失败', 'data' => $data, 'count' => $count]);
            break;
        default:
            return json(['code' => $code, 'msg' => $msg, 'data' => $data, 'count' => $count]);
    }
}
/**
 * 格式化字节大小
 * @param  number $size      字节数
 * @param  string $delimiter 数字和单位分隔符
 * @return string            格式化后的带单位的大小
 */
function format_bytes($size, $delimiter = '')
{
    $units = array('B', 'KB', 'MB', 'GB', 'TB', 'PB');
    for ($i = 0; $size >= 1024 && $i < 5; $i++) $size /= 1024;
    return round($size, 2) . $delimiter . $units[$i];
}
/**
 * 无限分级
 */
function grades($data, $father_id = 0, $level = 0)
{
    static $arr = array(); //静态数组
    static $farr = array();
    foreach ($data as $k1 => $v1) {
        if ($v1['parent_id'] > 0) {
            $farr[]['parent_id'] = $v1['parent_id'];
        }
    }
    foreach ($data as $k => $v) { //循环分类
        if ($v['parent_id'] == $father_id) { //找出顶级分类
            $v['level'] = $level; //将顶级分类赋予等级
            if (deep_in_array($v['id'], $farr) == true) {
                $v['is_son'] = 1;
            } else {
                $v['is_son'] = 0;
            }
            $arr[] = $v; //将顶级数组放入空数组
            grades($data, $v['id'], $level + 1); //将所有数组进行排序
        }
    }
    return $arr; //结果
}
function deep_in_array($value, $array)
{
    foreach ($array as $k => $v) {
        if (!is_array($v)) {
            if ($v == $value) {
                return true;
            } else {
                continue;
            }
        }
        if (in_array($value, $v)) {
            return true;
        } else if (deep_in_array($value, $v)) {
            return true;
        }
    }
    return false;
}
//查找所有文件
function read_all($dir)
{
    if (!is_dir($dir)) {
        return '';
    }
    $handle = opendir($dir);
    $list = [];
    if ($handle) {
        while (($fl = readdir($handle)) !== false) {
            //如果不加  $fl!='.' && $fl != '..'  则会造成把$dir的父级目录也读取出来
            if ($fl != '.' && $fl != '..') {
                $type =  substr(strrchr($fl, '.'), 1);
                $list[] = ['path' => $fl, 'type' => $type];
            }
        }
    }
    return $list;
}
//二维数组单显示
function arr_find($data, $name)
{
    foreach ($data as $k => $v) {
        if ($v['name'] == $name) {
            return $v['value'];
        }
    }
}
